﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using System.Collections.Generic;

namespace Ccit.OA.NewsCenter
{
    public partial class news_submit_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //类型
                hdType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
                //记录ID
                hdId.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "";
                //来源From 0：发布列表；1：管理列表
                hdFrom.Value = (null != Request.QueryString["From"]) ? Request.QueryString["From"].ToString() : "0";
                InitDDL();
                InitPage();
            }
            
        }

        private void InitDDL()
        {
            ColumnBLL oColumn=new ColumnBLL();
            string sType=hdType.Value;
            string sFrom = hdFrom.Value;
            List<MsColumn> lstColumn = new List<MsColumn>();
            if (sFrom == "0")
                lstColumn = oColumn.GetColPub(sType);
            else if (sFrom == "1")
                lstColumn = oColumn.GetColManage(sType);
            ddlColumn.DataSource = lstColumn;
            ddlColumn.DataTextField = "columnName";
            ddlColumn.DataValueField = "columnId";
            ddlColumn.DataBind();
        }

        private void InitPage()
        {
            string sId = hdId.Value;
            string sFrom=hdFrom.Value;
            if (sFrom == "1")
                btnSaveContiune.Style["display"] = "none";
            if (sId != "")
            {
                if (hdType.Value == "0")
                {
                    SubmitBLL oSubmit = new SubmitBLL();
                    MsNews msNews = oSubmit.GetNewsByNewsId(sId);
                    hdId.Value = msNews.NewsId;
                    txtNewsTitle.Value = msNews.NewsTitle;
                    hdNewsStatus.Value = msNews.NewsStatus;
                    ddlColumn.SelectedValue = msNews.ColumnNo;
                    if (msNews.TopFlag == "1")
                        chkTop.Checked = true;
                    if (msNews.PicFlag == "1")
                    {
                        chkHasImage.Checked = true;
                        haSelImage.Style["visibility"] = "visible";
                    }
                    else if (msNews.PicFlag == "0")
                    {
                        chkHasImage.Checked = false;
                        haSelImage.Style["visibility"] = "hidden";
                    }
                    if (msNews.CommentFlag == "1")
                        chkAllowDiscuss.Checked = true;
                    GetCheckedContentType(msNews.ContentFormat);
                    string sContentType = msNews.ContentFormat;
                    hdContentType.Value = sContentType;
                    if (sContentType == "1" || sContentType == "2")
                    {
                        InitOffice(sContentType);
                    }
                    else
                    {
                        ftbContent.Text = msNews.NewsContent;
                        txtUrl.Value = msNews.ConnectAddress;
                    }
                    ucAttach.EditShow("0010", sId);
                }
            }
            else
            {
                DisposePage();
            }
            string sScriptName = "news_submit_edit.aspx";
            string sServerName = "OfficeServer.aspx";
            string sHttpUrl = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.ServerVariables["SCRIPT_NAME"];
            sHttpUrl = sHttpUrl.Substring(0, sHttpUrl.Length - sScriptName.Length);
            string sServerUrl = sHttpUrl + sServerName;  //获取OfficeServer文件的完整路径
            hdWebUrl.Value = sServerUrl;
        }

        private void InitOffice(string sContentType)
        {
            hdRecordID.Value = "";
            hdTemplate.Value = "";
            if (sContentType == "1")
                hdFileType.Value = ".doc";
            else
                hdFileType.Value = ".xls";
            hdEditType.Value = "1,1";
            hdUserName.Value = "";
        }

        /// <summary>
        /// 获取选择的内容格式 0：HTML；1：WORD；2：EXCEL；3：URL；
        /// </summary>
        /// <returns>0：HTML；1：WORD；2：EXCEL；3：URL；</returns>
        private void GetCheckedContentType(string sContentType)
        {
            if (sContentType == "0")
                radHtml.Checked = true;
            else if (sContentType == "1")
                radWord.Checked = true;
            else if (sContentType == "2")
                radExcel.Checked = true;
            else if (sContentType == "3")
                radUrl.Checked = true;
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="sSaveType"></param>
        private string SaveNewsInfo(string sSaveType)
        {
            string sExeResult = "";
            MsNews msNews = new MsNews();
            string sContentType = GetCheckedContentType();
            string sNewsTitle = txtNewsTitle.Value.Trim();
            string sColumn = ddlColumn.SelectedValue;
            string sTopFlag = (chkTop.Checked) ? "1" : "0";
            string sAllowDiscuss = (chkAllowDiscuss.Checked) ? "1" : "0";
            string sHasImage = (chkHasImage.Checked) ? "1" : "0";
            msNews.NewsId = hdId.Value;
            msNews.ColumnNo = sColumn;
            msNews.NewsTitle = sNewsTitle;
            msNews.TopFlag = sTopFlag;
            msNews.PicFlag = sHasImage;
            msNews.CommentFlag = sAllowDiscuss;
            msNews.ContentFormat = sContentType;
            msNews.NewsStatus = hdNewsStatus.Value;
            msNews.NewsType = hdType.Value;           

            if (sContentType == "0") //HTML
            {
                string sNewsContent = ftbContent.Text;
                msNews.ConnectAddress = "";
                msNews.NewsContent = sNewsContent;
            }
            else if (sContentType == "1") //WORD
            {
                msNews.ConnectAddress = "";
                msNews.NewsContent = "";
            }
            else if (sContentType == "2")  //EXCEL
            {
                msNews.ConnectAddress = "";
                msNews.NewsContent = "";
            }
            else if (sContentType == "3")  //URL
            {
                string sUrl = txtUrl.Value.Trim();
                msNews.ConnectAddress = sUrl;
                msNews.NewsContent = "";
            }
            SubmitBLL oSubmit = new SubmitBLL();
            sExeResult = oSubmit.SaveNewsInfo(msNews,null);
            return sExeResult;
        }

        /// <summary>
        /// 获取选择的内容格式 0：HTML；1：WORD；2：EXCEL；3：URL；
        /// </summary>
        /// <returns>0：HTML；1：WORD；2：EXCEL；3：URL；</returns>
        private string GetCheckedContentType()
        {
            string sContentType="0";
            if (radHtml.Checked)
                sContentType = "0";
            else if (radWord.Checked)
                sContentType = "1";
            else if (radExcel.Checked)
                sContentType = "2";
            else if (radUrl.Checked)
                sContentType = "3";
            return sContentType;
        }

        /// <summary>
        /// Save Info
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkSave_Click(object sender, EventArgs e)
        {
            string sSaveType = hdSaveType.Value;
            string sType = hdType.Value;
            //if (sType == "0" || sType=="1")
            //{
                string sExeResult = SaveNewsInfo(sSaveType);
                if (sExeResult != "")
                {
                    hdId.Value = sExeResult;
                    ucAttach.SaveAttaches("0010", sExeResult);
                    Response.Write("<script language='javascript' type='text/javascript'>alert('保存成功！');</script>");
                    ucAttach.EditShow("0010", sExeResult);
                    if (sSaveType == "1")
                    {
                        InitPage();
                    }
                    if (sSaveType == "2") //保存并继续
                    {
                        DisposePage();
                    }
                    else if (sSaveType == "3")//保存并返回
                    {
                        string sPageParam = "Type=" + sType;
                        string sEncodeParam = Ccit.Utils.Base64Lib.Encode(sPageParam);
                        string sFrom=hdFrom.Value;
                        if(sFrom=="0")
                            Response.Write("<script language='javascript' type='text/javascript'>window.location.href='news_submit_list.aspx?" + sEncodeParam + "';</script>");
                        else if(sFrom=="1")
                            Response.Write("<script language='javascript' type='text/javascript'>window.location.href='news_list_manage.aspx?" + sEncodeParam + "';</script>");
                    }
                    SetImageShowByPicFlag();
                }
                else
                {
                    Response.Write("<script language='javascript' type='text/javascript'>alert('保存失败，请稍后再试！');</script>");
                }
            //}
        }

        private void SetImageShowByPicFlag()
        {
            if (chkHasImage.Checked)
                haSelImage.Style["visibility"] = "visible";
            else
                haSelImage.Style["visibility"] = "hidden";
        }

        private void DisposePage()
        {
            hdNewsStatus.Value = "0";
            radHtml.Checked = true;
            txtNewsTitle.Value = "";
            ddlColumn.SelectedValue="";
            chkTop.Checked = false;
            chkAllowDiscuss.Checked = false;
            chkHasImage.Checked = false;
            ftbContent.Text = "";
            haSelImage.Style["visibility"] = "hidden";
            hdId.Value = "";
            txtUrl.Value = "";
            ucAttach.EditShow("0010", "");
        }

        protected void lnkRefresh_Click(object sender, EventArgs e)
        {
            InitPage();
        }
    }
}
